package com.yx.cloud.fragment;//package com.yx.cloud.fragment;
//
//import android.annotation.SuppressLint;
//import android.content.Intent;
//import android.os.Bundle;
//import android.view.LayoutInflater;
//import android.view.View;
//import android.view.ViewGroup;
//import android.widget.FrameLayout;
//import android.widget.LinearLayout;
//import android.widget.ListView;
//
//import androidx.annotation.NonNull;
//import androidx.annotation.Nullable;
//import androidx.fragment.app.Fragment;
//
//import com.tuya.smart.sdk.bean.message.MessageType;
//import com.yx.cloud.Interface.BounceCallBack;
//import com.yx.cloud.Interface.EventForwardingHelper;
//import com.yx.cloud.MyApplication;
//import com.yx.cloud.R;
//import com.yx.cloud.activity.MessageActivity;
//import com.yx.cloud.activity.MessageParticularsActivity;
//import com.yx.cloud.adapter.MessageAdapter;
//import com.yx.cloud.header.NormalBounceHandler;
//import com.yx.cloud.utils.RomUtils;
//import com.yx.cloud.utils.ToastUtil;
//import com.yx.cloud.view.BounceLayout;
//import com.yx.cloud.view.DefaultFooter;
//import com.yx.cloud.view.DefaultHeader;
//import com.yx.robot.bean.message.IotMessage;
//import com.yx.cloud.AppContext;
//import com.iot.common.utils.Event;
//import com.yx.robot.sdk.req.DeleteNotificationParam;
//import com.yx.robot.sdk.req.GetNotificationParam;
//import com.iot.common.utils.LogUtils;
//
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * 消息设备
// */
//
///**
// * 消息设备
// */
//public class MessageEquipmentFragment extends Fragment implements MessageActivity.MessageEdit {
//    private final static String TAG = "MessageEquipmentFragment";
//    //    private SwipeRefreshView srv_inform;
//    boolean mIsRefresh = false;
//    private ListView mListView;
//    private LinearLayout LlNoMessage;
//    private MessageAdapter messageAdapter;
//    private List<IotMessage> mMessageList;
//    private BounceLayout bl_message;
//    private FrameLayout fl_message_container;
//    private EventForwardingHelper eventForwardingHelper = new EventForwardingHelper() {//自定义事件分发处理
//        @Override
//        public boolean notForwarding(float downX, float downY, float moveX, float moveY) {
//            return true;
//        }
//    };
//
//    @Nullable
//    @Override
//    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        View view = inflater.inflate(R.layout.messageinform_fragment, container, false);
//        initView(view);
//        requestNotification(0, 10, true);
//        return view;
//    }
//
//    public void intoEditMode() {
//        ((MessageActivity) getActivity()).setSelectAll(messageAdapter.isAllSelected());
//        messageAdapter.editMode(true);
//        bl_message.setEventForwardingHelper(null);
//    }
//
//    public void exitEditMode() {
//        messageAdapter.editMode(false);
//        bl_message.setEventForwardingHelper(eventForwardingHelper);
//    }
//
//    public boolean pickAllItem() {
//        if (messageAdapter.isAllSelected()) {
//            messageAdapter.pickAll(false);
//            return false;
//        } else {
//            messageAdapter.pickAll(true);
//            return true;
//        }
//    }
//
//    public List<String> getDeleteItem() {
//        List<IotMessage> selectedMessage=messageAdapter.getSelectedMessages();
//        List<String> ids=new ArrayList<>();
//        for(int i=0;i<selectedMessage.size();i++){
//            ids.add(selectedMessage.get(i).getId());
//        }
//        return ids;
//    }
//
//    @Override
//    public void updateData() {
//        if(messageAdapter.getCount()<=10){
//            requestNotification(0, 10, true);
//        }else{
//            requestNotification(0, messageAdapter.getCount(), true);
//        }
//    }
//
//    @Override
//    public List<IotMessage> getMessageList() {
//        return messageAdapter.getMessageList();
//    }
//
//    @Override
//    public void onDataUpdate(Event event) {
//        if (((Integer) event.getExtra()) != 2) {
//            return;
//        }
//        if (event.isResult()) {
//            List<IotMessage> result = (List<IotMessage>) event.getContent();
//            parceMessage(result, mIsRefresh);
//        } else {
//            LlNoMessage.setVisibility(View.VISIBLE);
//        }
//    }
//
//    @Override
//    public void deleteSelectedItem() {
//        List<String> mIds = getDeleteItem();
//        if (mIds == null || mIds.size() == 0) {
//            ToastUtil.showShort(getActivity(), R.string.select_message_to_delete);
//            return;
//        }
//        deleteNotification(mIds);
//    }
//
//    private void deleteNotification(List<String> mIds) {
//        LogUtils.d(" 消息: =======deleteNotification======== :");
//        DeleteNotificationParam param = new DeleteNotificationParam();
//        param.setIds(mIds);
//        AppContext.getService().deleteNotification(param);
//    }
//
//    @SuppressLint("LongLogTag")
//    private void initView(View view) {
//        LlNoMessage = view.findViewById(R.id.ll_noMessage);
//        LlNoMessage.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (RomUtils.isFastClick()) {
//                    return;
//                }
//                requestNotification(0, 10, true);
//            }
//        });
//
//        mListView = view.findViewById(R.id.messageListView);
//        mMessageList = new ArrayList<>();
//        messageAdapter = new MessageAdapter(getActivity(), mMessageList, MessageType.MSG_FAMILY);
//        mListView.setAdapter(messageAdapter);
//        messageAdapter.setItemListener(new MessageAdapter.ItemListener() {
//            @Override
//            public void onItemClick(int position) {
//                if (!messageAdapter.isEditMode()) {
//                    Intent intent = new Intent(getActivity(), MessageParticularsActivity.class);
//                    IotMessage messageBean = messageAdapter.getItem(position);
//                    intent.putExtra("title", getString(R.string.device));
//                    intent.putExtra("type", messageBean.getTitle());
//                    intent.putExtra("content", messageBean.getContent());
//                    intent.putExtra("time", messageBean.getDateTime());
//                    intent.putExtra("icon", messageBean.getIcon());
//                    startActivity(intent);
//                }
//            }
//
//            @Override
//            public void onItemDelete(int position) {
//            }
//
//            @Override
//            public void onItemCheckChanged(boolean isCheck) {
//                ((MessageActivity) getActivity()).setSelectAll(messageAdapter.isAllSelected());
//            }
//
//        });
//
//        bl_message = view.findViewById(R.id.bl_message);
//        fl_message_container = view.findViewById(R.id.fl_message_container);
//        bl_message.setBounceHandler(new NormalBounceHandler(), mListView);//设置滚动冲突的控制类
//        bl_message.setEventForwardingHelper(new EventForwardingHelper() {//自定义事件分发处理
//            @Override
//            public boolean notForwarding(float downX, float downY, float moveX, float moveY) {
//                return true;
//            }
//        });
//        bl_message.setHeaderView(new DefaultHeader(getActivity()), fl_message_container);//设置刷新头，null意味着没有刷新头，不调用该函数意为着空
//        bl_message.setFooterView(new DefaultFooter(getActivity()), fl_message_container);
//        bl_message.setBounceCallBack(new BounceCallBack() {
//            @Override
//            public void startRefresh() {
//                requestNotification(0, 10, true);
//            }
//
//            @Override
//            public void startLoadingMore() {
//                requestNotification(messageAdapter.getCount(), 10, false);
//            }
//        });
//
///*
//        srv_inform = view.findViewById(R.id.srv_msg);
//        srv_inform.setItemCount(7);
//        srv_inform.setEnabled(true);
//        // 设置上拉加载更多
//        srv_inform.setOnLoadMoreListener(new SwipeRefreshView.OnLoadMoreListener() {
//
//            @Override
//            public void onLoadMore() {
//                LogUtils.i( "on onLoadMore ");
//                requestNotification(messageAdapter.getCount(), 10, false);
//            }
//        });
//        srv_inform.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                LogUtils.i( "on refresh ");
//                requestNotification(0, 10, true);
//            }
//        });
//*/
//
//    }
//
//
//    @SuppressLint("LongLogTag")
//    private void requestNotification(int offset, int limit, final boolean isRefresh) {
//
//        mIsRefresh = isRefresh;
//        mMessageList = new ArrayList<>();
//        LogUtils.d(mMessageList.size() + " 消息: =======requestNotification======== :" + mMessageList.size());
//
//        GetNotificationParam param = new GetNotificationParam();
//        param.setOffset(offset);
//        param.setLimit(limit);
//        param.setType(2);
//        AppContext.getService().getNotification(param);
//
//    }
//
//    private void parceMessage(List<IotMessage> result, boolean isRefresh) {
//        LogUtils.i("getMessageList total--> : " + result.size());
//        List<IotMessage> tempList = result;
//        if (isRefresh) {
//            messageAdapter.updateMessage(tempList);
//            bl_message.setRefreshCompleted();
//        } else {
//            //TODO 这里不确定会不会取到错误数据 有新消息或者消息被删了  下标有变化 比如 以前是取10-20  新来了一条消息后 继续取10-20 其实取到的是以前的9-19
//            //可能需要做去重和重定位
//            messageAdapter.addMessage(tempList);
//            bl_message.setLoadingMoreCompleted();
//        }
//
//        if (tempList == null || tempList.size() == 0) {
//            if (messageAdapter.getCount() == 0) {
//                fl_message_container.setVisibility(View.GONE);
//                LlNoMessage.setVisibility(View.VISIBLE);
//                //ToastUtil.showShort(MyApplication.get(), R.string.no_data);
//            } else {
//                ToastUtil.showShort(MyApplication.get(), R.string.No_more_history);
//            }
//        } else {
//            fl_message_container.setVisibility(View.VISIBLE);
//            LogUtils.i("getMessageList size--> : " + tempList.size());
//            LlNoMessage.setVisibility(View.GONE);
//        }
//    }
//}
